/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.fw.types;

import javax.xml.bind.annotation.XmlRootElement;

import net.interknit.knitter.fw.util.ReflectionHelper;

@XmlRootElement(name = "net.interknit.knitter.fw.types.AnyCollection")
public class AnyCollection
{
	/**
	 * The underlying state maintained by this adapter
	 */
	private java.util.Collection<Any> state = defaultState();

	/**
	 * Private method to provide the "default" state for this adapter
	 */
	private java.util.Collection<Any> defaultState()
	{
		return state != null ? state : (state = new java.util.ArrayList<Any>());
	}

	/**
	 * Private method to provide the "default" value for this adapter
	 */
	private java.util.Collection<java.lang.Object> defaultValue(java.util.Collection<java.lang.Object> v)
	{
		return v != null ? v : new java.util.ArrayList<java.lang.Object>();
	}

	/**
	 * Default no-arg constructor
	 */
	public AnyCollection() { ; }

	/**
	 * Construct this instance from the Object parameter.
	 *
	 * @throws ClassCastException if the parameter obj is not of type java.util.ArrayList<java.lang.Object>
	 */
	public AnyCollection(java.lang.Object obj)
	{
		this((java.util.ArrayList<java.lang.Object>) obj);
	}

	/**
	 * Constructor to accept the user values and convert it to the underlying state.
	 * In other words, this constructor provides a way for "wrapping" the user value into this adapter.
	 * This constructor is also known as 'value constructor'
	 */
	public AnyCollection(java.util.Collection<java.lang.Object> value)
	{
		for(java.lang.Object v : defaultValue(value)) defaultState().add(new Any(v));
	}

	/**
	 * Adapter method to convert the underlying state back to user value.
	 * In other words, this method provides a way for "unwrapping" the user value from this adapter that
	 * was previously wrapped using the value constructor.
	 */
	public <T extends java.util.Collection<java.lang.Object>> T unwrap(Class<T> type)
	{
		T list = ReflectionHelper.createInstance(type);
		for(Any a : defaultState()) list.add(a.unwrap());
		return list;
	}


	/**
	 * Return the underlying state of this adapter.
	 * Note: the underlying state must not be modified or manipulated as part of this method,
	 * this method should act as a simple getter method for the underlying state.
	 *
	 * @return the underlying state of this adapter
	 */
	public java.util.Collection<Any> getCollection()
	{
		return defaultState();
	}

	/**
	 * Sets the underlying state of this adapter with the given state parameter.
	 * Note: the underlying state must not be modified or manipulated as part of this method,
	 * this method should act as a simple setter method for the underlying state.
	 *
	 * @param state the underlying state to set
	 */
	public void setCollection(java.util.Collection<Any> state)
	{
		this.state = state;
	}
}
